// RibbonExplorer.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "RibbonExplorer.h"
#include "RibbonCompiler.h"
#include "MainView.h"
#include "RibbonResultView.h"
#include "TabContainer.h"
#include "CompilerOutputTabView.h"
#include <commctrl.h>
#include <memory>

#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(lib, "comctl32.lib")

CRibbonExplorer *CRibbonExplorer::s_pTheRibbonExplorer = 0;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);


int APIENTRY wWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);

	CRibbonExplorer ribbonExplorer(hInstance, lpCmdLine, nCmdShow);
	return ribbonExplorer.Run();
}

int CRibbonExplorer::Run()
{
	MSG msg;
	HACCEL hAccelTable;

	CoInitialize(NULL);

	INITCOMMONCONTROLSEX initCommonControls = {sizeof(INITCOMMONCONTROLSEX), ICC_TAB_CLASSES | ICC_STANDARD_CLASSES | ICC_LINK_CLASS };

	InitCommonControlsEx(&initCommonControls);

	// Initialize global strings
	LoadString(m_hInstance, IDS_APP_TITLE, m_szTitle, MAX_LOADSTRING);
	LoadString(m_hInstance, IDC_RIBBONEXPLORER, m_szWindowClass, MAX_LOADSTRING);
	MyRegisterClass();

	// Perform application initialization:
	if (!InitInstance())
	{
		return -1;
	}

	hAccelTable = LoadAccelerators(m_hInstance, MAKEINTRESOURCE(IDC_RIBBONEXPLORER));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	CoUninitialize();
	return (int) msg.wParam;
}


ATOM CRibbonExplorer::MyRegisterClass()
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= m_hInstance;
	wcex.hIcon			= LoadIcon(m_hInstance, MAKEINTRESOURCE(IDI_RIBBONEXPLORER));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_RIBBONEXPLORER);
	wcex.lpszClassName	= m_szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_RIBBONEXPLORER));

	return RegisterClassEx(&wcex);
}


BOOL CRibbonExplorer::InitInstance()
{
	m_hWnd = CreateWindow(m_szWindowClass, m_szTitle, WS_OVERLAPPEDWINDOW,
	  CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, m_hInstance, NULL);

	if (!m_hWnd)
	{
		return FALSE;
	}

	CComObject<CMainView> *pApplication = NULL;
	CComObject<CMainView>::CreateInstance(&pApplication); // refcnt zero
	m_pMainView = pApplication;
	HRESULT result = CMainView::Create(pApplication, m_hWnd, m_hInstance, GetModuleHandle(NULL), L"APPLICATION_RIBBON"); 
    if (FAILED(result))
		return FALSE;
	
	ShowWindow(m_hWnd, m_nCmdShow);
	UpdateWindow(m_hWnd);

	return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(CRibbonExplorer::s_pTheRibbonExplorer->m_hInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), CRibbonExplorer::s_pTheRibbonExplorer->m_hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;

	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;

	case WM_DESTROY:
		CRibbonExplorer::s_pTheRibbonExplorer->Exit();
		PostQuitMessage(0);
		break;
	default:
		if (CRibbonExplorer::s_pTheRibbonExplorer != 0) 
		{
			LRESULT result = CRibbonExplorer::s_pTheRibbonExplorer->ProcessMessege(hWnd, message, wParam, lParam);
			if (result != 0)
				return result;
		}

		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}


void CRibbonExplorer::ResizeChildWindows(RECT parentRect)
{
	m_pMainView->UpdateWindowSise(parentRect);
}

void CRibbonExplorer::Exit()
{
	//destroy child windows
}

LRESULT CRibbonExplorer::ProcessMessege(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
		case WM_SIZE:
		{
			if (CRibbonExplorer::s_pTheRibbonExplorer->m_pMainView == 0 || !CRibbonExplorer::s_pTheRibbonExplorer->m_pMainView->IsMainWindowValid() )
				break;
			
			RECT rect;
			GetClientRect(hWnd, &rect);
			ResizeChildWindows(rect);
		}
		break;
	
	case WM_NOTIFY:
		{
			LPNMHDR lpnmhdr = (LPNMHDR)lParam;

            if (lpnmhdr->code == TCN_SELCHANGE)
            {
				m_pMainView->TabChanged();
            }
		}
	}

	return 0;
}